//
//  ViewControllerForDestinationPhoto.m
//  ProjectA
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewControllerForDestinationPhoto.h"
#import "ViewControllerForPhotoPlayer.h"

#import "CellForDestinationPhoto.h"

#import "ModelForDestinationPhoto.h"


@interface ViewControllerForDestinationPhoto ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSString *next_start;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrForData;

@end

@implementation ViewControllerForDestinationPhoto
- (void)dealloc {
    
    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_next_start release];
    [_collectionView release];
    [_arrForData release];
    
    [_titleForBar release];
    [_url release];
    [super dealloc];
}
- (void)setNightDayModel {
    [super setNightDayModel];

}
- (void)setLightDayModel {
    [super setLightDayModel];

}
- (void)viewWillAppear:(BOOL)animated {
    
    if (ISNIGHT) {
        [self setNightDayModel];
        self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
    } else {
        [self setLightDayModel];
        self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 加载数据菊花
    [self.view addSubview:[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame]];
    [self handleData];
    
    [self createCollectionView];
    
    [self settingsNavigationController];

}
- (void)settingsNavigationController {
    self.navigationItem.title = self.titleForBar;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];
    
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    [flowLayout release];
    
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //http://api.breadtrip.com//destination/place/3/9990/photos/?gallery_mode=1&count=18&start=0
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSString *url = [NSString stringWithFormat:@"%@photos/?gallery_mode=1&count=18&start=%@", self.url, self.next_start];
            
            [NetWorkHandler networkingWithUrl:url comleteHandle:^(id result , NSData *data) {
                
                NSArray *arrForData = [result objectForKey:@"items"];
                self.next_start = [result objectForKey:@"next_start"];
                for (NSDictionary *dic in arrForData) {
                    ModelForDestinationPhoto *model = [[ModelForDestinationPhoto alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.arrForData addObject:model];
                    [model release];
                }
                [self.collectionView reloadData];
                [self.collectionView.mj_footer endRefreshing];
            }];
            
        });
        
    }];

    
    [self.collectionView registerClass:[CellForDestinationPhoto class] forCellWithReuseIdentifier:@"pool"];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForDestinationPhoto *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    ModelForDestinationPhoto *model = [self.arrForData objectAtIndex:indexPath.row];
    [cell passData:model];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForData.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake((self.view.frame.size.width - 10) / 3.f, (self.view.frame.size.width - 10) / 3.f);;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ViewControllerForPhotoPlayer *photoPlayer = [[ViewControllerForPhotoPlayer alloc] init];
    
    [photoPlayer passData:self.arrForData indexPath:indexPath];
    photoPlayer.url = self.url;
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:photoPlayer animated:YES];
    [photoPlayer release];
    self.hidesBottomBarWhenPushed = NO;


}
- (void)handleData {
    
    self.arrForData = [NSMutableArray array];
    [NetWorkHandler networkingWithUrl:self.url comleteHandle:^(id result, NSData *data) {
       
        NSArray *arrForData = [result objectForKey:@"items"];
        self.next_start = [result objectForKey:@"next_start"];
        for (NSDictionary *dic in arrForData) {
            ModelForDestinationPhoto *model = [[ModelForDestinationPhoto alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForData addObject:model];
            [model release];
        }
        
        [self.collectionView reloadData];
        [[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame] didFinishLoad];
        
    }];
}



- (void)houtuiAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = YES;
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
